package com.ocom.payconfig.controller;


import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfMealInfo;
import com.ocom.common.entity.card.EntityXfMealTime;
import com.ocom.common.request.payconfig.GetMealDetailListBRequest;
import com.ocom.common.request.payconfig.GetMealDetailListRequest;
import com.ocom.common.request.web.ConsTimeConfigOnShowRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.XfMealInfoService;
import com.ocom.payconfig.service.XfMealTimeService;
import com.ocom.payconfig.vo.MealDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 3.1.获取餐段详情列表信息(getMealDetailList)
 */
@RestController
public class MealDetailListController {
    @Autowired
    private XfMealInfoService mealInfoService;
    @Autowired
    private XfMealTimeService mealTimeService;

    //3.1.获取餐段详情列表信息(getMealDetailList)
    @PostMapping(PayConfigUrl.Url_getMealDetailList)
    public Result getMealDetailList(@RequestBody GetMealDetailListRequest request) {
        if (request.getId() != null && request.getId() != -1) {
        }
        List<EntityXfMealInfo> entityXfMealInfos = mealInfoService.GetMealNameInfo(request.getId(), request.getCom_id(), request.getMeal_no(), null);
        List<MealDetailVo> mealDetailVos = new ArrayList<>();
        if (entityXfMealInfos != null) {
            for (int i = 0; i < entityXfMealInfos.size(); i++) {
                MealDetailVo mealDetailVo = new MealDetailVo();
                EntityXfMealInfo entityXfMealInfo = entityXfMealInfos.get(i);
//                mealDetailVo.setId(entityXfMealInfo.getId());
                mealDetailVo.setComId(entityXfMealInfo.getComId());
                mealDetailVo.setMealName(entityXfMealInfo.getMealName());
                mealDetailVo.setMealNo(entityXfMealInfo.getMealNo());
                mealDetailVo.setCreateTime(entityXfMealInfo.getCreateTime());

                ConsTimeConfigOnShowRequest consTimeConfigOnShowRequest = new ConsTimeConfigOnShowRequest();
                consTimeConfigOnShowRequest.setComId(Long.valueOf(entityXfMealInfo.getComId()));
                consTimeConfigOnShowRequest.setMealNo(entityXfMealInfo.getMealNo());
                if (request.getAcc_type_id() != null && request.getAcc_type_id() != -1) {
                    consTimeConfigOnShowRequest.setAccTypeId(request.getAcc_type_id());
                }
                if (request.getPoint_id() != null && request.getPoint_id() != -1) {
                    consTimeConfigOnShowRequest.setPointId(request.getPoint_id());
                }
                //根据com_id+meal_no；
                List<EntityXfMealTime> entityXfMealTimes = mealTimeService.GetMealTimeInfo(Long.valueOf(entityXfMealInfo.getComId()), consTimeConfigOnShowRequest);
                if (entityXfMealTimes != null && entityXfMealTimes.size() > 0) {
                    EntityXfMealTime entityXfMealTime = entityXfMealTimes.get(0);
                    mealDetailVo.setBeginTime(entityXfMealTime.getBeginTime());
                    mealDetailVo.setEndTime(entityXfMealTime.getEndTime());
                    mealDetailVo.setAccTypeId(entityXfMealTime.getAccTypeId());
                    mealDetailVo.setPointId(entityXfMealTime.getPointId());
                    mealDetailVo.setAllowDays(entityXfMealTime.getAllowDays());
                    mealDetailVo.setBeforeHour(entityXfMealTime.getBeforeHour());
                    mealDetailVo.setAllowMinute(entityXfMealTime.getAllowMinute());
                    mealDetailVo.setId(entityXfMealTime.getId());
//                    mealDetailVo.setIfDays(entityXfMealTim);
                }
                mealDetailVos.add(mealDetailVo);
            }
        }
        return ResultUtil.success(mealDetailVos);
    }

    @PostMapping(PayConfigUrl.Url_getMealDetailListB)
    public Result getMealDetailListB(@RequestBody GetMealDetailListBRequest request) {
        return ResultUtil.success(this.mealInfoService.getMealDetailListB(request.getComId(), request));
    }

}
